<template >
  <div style="height: 100%;">
    <el-card class="box-card" shadow="hover">
      <!-- 搜索表单-->
      <el-form :inline="true">
          <el-form-item>
              <el-button
              icon="el-icon-plus" 
              size="mini" 
              type="primary"
              @click="dialogTitle = '添加banner'
              ,dialogVisible = true
              ,form = {}
              ,img = false">
                添加banner
              </el-button>
          </el-form-item>
      </el-form>
      <!--banner列表-->
     <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
      prop="id"
      label="id"
      align="center">
      </el-table-column>
      <el-table-column
      prop="imgUrl"
      label="商品缩略图"
      align="center">
        <template slot-scope="scope">
          <img v-if="scope.row.imgUrl" :src="scope.row.imgUrl" class="url">
        </template>
      </el-table-column>
      <el-table-column
      prop="seqNumber"
      align="center"
      label="顺序">
      </el-table-column>
      <el-table-column
      width="100"
      fixed="right"
      align="center"
      label="操作">
        <template slot-scope="scope">
            <i class="el-icon-edit" style="font-size: 12px;color: #409EFF;cursor:pointer;" @click="updateBanner(scope.row.id)">编辑</i>
            <i class="el-icon-delete" style="font-size: 12px;color: #F56C6C;cursor:pointer;" @click="remove(scope.row.id)">删除</i>
        </template>
      </el-table-column>
    </el-table>
    <!--分页栏-->
    <el-pagination
      background
      align="center"
      @size-change="handleSizeChange($event,111)"
      @current-change="data()"
      :current-page.sync="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </el-card>
    <!--添加框/编辑框-->
    <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="50%"
    :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="商品缩略图">
          <el-upload
          class="Banner-uploader"
          :action="uploadUrl"
          :headers="headers"
          :show-file-list="false"
          :on-success="handleBannerSuccess"
          :before-upload="beforeBannerUpload">
            <img v-if="img" :src="form.imgUrl" class="Banner">
            <i v-else class="el-icon-plus Banner-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="顺序">
          <el-input 
          type="number"
          v-model="form.seqNumber">
          </el-input>
        </el-form-item>
        <el-form-item label="html内容">
          <div style="height: 18em;">
              <Editor v-model="form.content" placeholder="请输入内容" />
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Editor from '@/components/Editor'
import { list,add,del,update,banner } from '@/api/app_pages/banner'
import { getToken } from '@/utils/auth'
export default {
    components:{
      Editor 
    },
    data(){
      return {
        tableData: [],
        form: {
        },
        img: false,
        size: 20,
        currentPage: 1,
        total: 0,
        dialogVisible: false,
        prev:{},
        dialogTitle:'',
        uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload",
        headers: {
          Authorization: 'Bearer ' + getToken()
        }
      }
    },
    created(){     
      this.data()    
    },
    methods:{
      data(){
        list(this.size,this.currentPage).then(res => {
          this.tableData = res.rows
          this.total = parseInt(res.total)
        })
      },
      handleSizeChange(val,e) {
        this.size = val;
        this.data()
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      handleBannerSuccess(res, file) {
        this.form.imgUrl = res.fileName
        this.img = true
      },
      beforeBannerUpload(file) {
        const isImg = file.type.indexOf("image/")==0;

        if (!isImg) {
          this.$message.error('图片格式不正确!');
        }

        return isImg;
      },
      updateBanner(id){
        this.dialogTitle = '编辑banner'
        this.dialogVisible = true
        this.form.id = parseInt(id);  
        banner(this.form.id).then(res => {
            this.form = res.data 
            this.img = true
          })
      },
      submmit(){
        if(this.form.seqNumber == ''){
          this.$message('顺序不能为空')
          return
        }
        if(this.form.content == ''){
          this.$message('html内容不能为空')
          return
        }
        if(this.dialogTitle == '编辑banner'){
            new Promise((resolve,reject) =>{
              update(this.form).then(res => {
                this.dialogVisible = false
                resolve()
              })
            }).then(res => {
              this.data()
            })
          
        }else{
          new Promise((resolve,reject) =>{
            add(this.form).then(res => {
                this.dialogVisible = false
                resolve()
              })  
            }).then(res => {
              this.data()
            })
        }
      },
      remove(id){
        new Promise((resolve,reject) =>{
            del(id).then(res => {
              resolve()
            })  
        }).then(res => {
          this.$message('删除成功')
          this.data()
        })   
      }
    }
}
</script>
<style>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    margin: 0 auto;
  }

  .Banner-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .Banner-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .Banner-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .Banner {
    width: 178px;
    height: 178px;
    display: block;
  }
  .url {
        width: 50px;
        height: 50px;
        display: block;
        margin: 0 auto;
    }
 
</style>